//
//  HJWelcomeViewController.swift
//  HJWeiBo
//
//  Created by 夏航军 on 16/9/25.
//  Copyright © 2016年 夏航军. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

private let bottomMargin:CGFloat = 100

class HJWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
 self.view.backgroundColor  = UIColor.white
        setupUI()
    }

    //执行动画效果要放在已经显示的视图上执行
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }

    func startAnimation() {
        
        
        //更新约束
        let offSetY = -(KScreenH - bottomMargin - 85)
        
        iconImageView.snp.updateConstraints { (make) in
            
            make.bottom.equalTo(view).offset(offSetY)
        }
        
        UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: [], animations: {
            
            //frame是在layoutSubviews方法中根据已经设置好的约束来自动修改控件的frame
            //提前布局子视图   -> layoutSubviews -> 动画时间才会奏效
            self.view.layoutSubviews()
            
            }) { (_) in
                
                UIView.animate(withDuration: 0.5, animations: {
                    
                    self.welcomeLable.alpha = 1.0
                    
                    }, completion: { (_) in
                        
                        //切换到TabbarController
                        NotificationCenter.default.post(name: NSNotification.Name(KchangeRootViewController), object: nil)
                })
        }
        
    }
    
    func setupUI() {
        
        view.addSubview(iconImageView)
        view.addSubview(welcomeLable)
        
       // print(welcomeLable)
        iconImageView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-bottomMargin)
            make.size.equalTo(CGSize(width: 85, height: 85))
            
        }
        
        welcomeLable.snp.makeConstraints { (make) in
            make.top.equalTo(iconImageView.snp.bottom).offset(20)
            make.centerX.equalTo(view)
        }
        
        //给头像赋值
        iconImageView.sd_setImage(with: HJUserAccountViewModel.sharedAccountViewModel.iconURL)
    }
    
    lazy var  iconImageView : UIImageView = {
        let iv = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big") )
        
        iv.cornerRadius = 42
        
        iv.layer.borderWidth = 1
        //边线颜色
        iv.layer.borderColor = UIColor.orange.cgColor

        return iv
    }()
    
    lazy var welcomeLable : UILabel = {
        
        let lable = UILabel(title: "欢迎归来", textColor: UIColor.darkGray, fontSize: 16)
        //刚开始显示的时候透明度为0,让他不显示
        lable.alpha = 0
        
        return lable
        
    }()
    
}
















